package io.xuan.modules.app.controller;


import io.xuan.modules.app.service.CarInfoService;
import io.xuan.modules.app.service.CarLineService;
import io.xuan.modules.app.service.CarStationService;
import io.xuan.modules.app.entity.CarLineEntity;
import io.xuan.modules.app.entity.CarInfoEntity;
import io.xuan.common.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;

@Api(tags = "用户端(学生)——获得车辆站 点信息")
@RestController
@RequestMapping("app/car")
public class AppCarInfoController {

    @Autowired
    private CarLineService carLineService;
    @Autowired
    private CarStationService carStationService;

    @GetMapping("/station")
    @ApiOperation("获取校车站点信息")
    public R carStation() { //R是一个工具类，封装要返回的数据

        return R.ok().put("carStation", carStationService.getList());//这里会调用服务的getList读取数据库中的值返回。
    }


    @Autowired
    private CarInfoService carInfoService;

    @GetMapping("/line")
    @ApiOperation("获取车相关的信息")
    public R carInfo(@RequestParam("lineString") String Id) {
        Map<String, Object> resultMap = new HashMap<>();
        String[] ids = Id.split(",");
        for (String id : ids) {
            List<CarInfoEntity> carInfoList = carInfoService.AllCarInfo(Integer.parseInt(id));
            List<CarLineEntity> carLineList = carLineService.CarLine(Integer.parseInt(id));

            resultMap.put("carInfo" + id, carInfoList);
            resultMap.put("carLine" + id, carLineList);
        }

        return R.ok().put("result", resultMap);
    }

    @PutMapping("/update")
    @ApiOperation("更新车辆相关信息")
    public R carUpdate(@RequestParam("uid") String uId, @RequestParam("lat") String carLat,
                       @RequestParam("lng") String catLng) {
        Map<String, String> resultMap = new HashMap<>();

        resultMap.put("car_lat", String.valueOf(carLat));
        resultMap.put("car_lng", String.valueOf(catLng));
        resultMap.put("driver_id", String.valueOf(uId));
//        for (Object test : resultMap.keySet()) {
//            System.out.println(test);
//        }
        carInfoService.updateCarLocation(resultMap);
        return R.ok();
    }


    @GetMapping("/lineAll")
    @ApiOperation("获取所有路线信息")
    public R carAllLine() {
        List<CarLineEntity> carLineList = carLineService.AllCarLine();

        return R.ok().put("result", carLineList);
    }


    @GetMapping("/carLocation")
    @ApiOperation("获取指定车辆ID的车辆位置")
    public R carLocation(@RequestParam("lineId") Integer Id) { //R是一个工具类，封装要返回的数据
        return R.ok().put("carLocation", carInfoService.carLocation(Id));
    }






}
